﻿#include "produce_dialog.h"
#include "ui_produce_dialog.h"
#include <QSqlQuery>
#include <QCompleter>
#include <QDebug>
#include <QMessageBox>
//连续购买商品界面
produce_dialog::produce_dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::produce_dialog)
{
    ui->setupUi(this);
    //对ComboBox进行默认数据的插入（提取表格produce中的商品姓名信息）
    QStringList str;
    str.append(" ");
    QSqlQuery query;
    query.exec(tr("select p_name from produce"));
    while(query.next())
    {
        QString pName = query.value(0).toString();
        str.append(pName);
    }
    //把str中的数据插入控件ComboBox中
    QCompleter* completer = new QCompleter(str, this);
    // ui->userBox->clear();
    ui->proCBox->addItems(str);
    ui->proCBox->setCompleter(completer);
    //单价禁止修改，保证输入的数量num为有效数据
    ui->editPrice->setEnabled(false);
    QIntValidator *validator = new QIntValidator(0, 100, this);
    ui->editNum->setValidator(validator);

}

produce_dialog::~produce_dialog()
{
    delete ui;
}

//信号
void produce_dialog::on_addCarBtn_clicked()
{
    if(ui->editNum->text().isEmpty()){
       QMessageBox::about(NULL, "警告", "数量不能为空！");
       ui->editNum->setFocus();
    }else
    {
       emit addPro(ui->proCBox->currentText(), ui->editPrice->text(), ui->editNum->text());
    }
}

//选取不同的商品更新界面单价数据
void produce_dialog::on_proCBox_currentIndexChanged(int index)
{
    QSqlQuery query;
    if(ui->proCBox->currentIndex() != 0){
        QString proName = ui->proCBox->currentText();
        QString sql = "select p_sell_price from produce where p_name='" +proName+ "'";
        query.exec(sql);
        query.next();
        QString price = query.value(0).toString();
        ui->editPrice->setText(price);
    }
}

void produce_dialog::on_SettBtn_clicked()
{
    this->close();
}

